package cn.com.dhcc.turbo.web.controller.registry;

import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.app.pub.core.annotation.TurboMenu;
import cn.com.dhcc.app.pub.core.annotation.TurboMenu.LEAF_MENU;
import cn.com.dhcc.turbo.entity.registry.MonitorQuota;
import cn.com.dhcc.turbo.entity.registry.MonitorQuotaConf;
import cn.com.dhcc.turbo.entity.registry.MonitorRule;
import cn.com.dhcc.turbo.service.monitor.MonitorQuotaStatic.ALERT_PIPELINE;
import cn.com.dhcc.turbo.service.registry.MonitorQuotaService;
import cn.com.dhcc.turbo.service.registry.MonitorRuleService;

/**
 * 监控指标Controller
 * 
 * @日期：2014-6-12 下午3:18:26
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Controller
@RequestMapping("/registry")
@TurboMenu(belongToMenu=LEAF_MENU.QUOTA_MGR)
public class MonitorQuotaController extends BaseController {
	@SuppressWarnings("deprecation")
	Logger logger = Log.getExistLogger(LOGTYPE.SAI);
	@Autowired
	private MonitorQuotaService mqService;
	@Autowired
	private MonitorRuleService ruleService;

	/**
	 * 获取监控指标列
	 * 
	 * @param model
	 * @param pager
	 * @param searchCode
	 * @param searchName
	 * @return
	 */
	@RequestMapping(value = "/monitor_quota_list", method = RequestMethod.GET)
	public String list(Model model) {

		try {
			List<MonitorQuota> list = mqService.list();
			EnumSet<ALERT_PIPELINE> alertPipelineEnum = EnumSet.allOf (ALERT_PIPELINE. class);
			model.addAttribute(DATA_KEY, list);
			model.addAttribute("alertPipelineEnum", alertPipelineEnum);
		} catch (Exception e) {
			logger.error("获取监控指标列表失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return "registry/monitor_quota_list";
	}

	@RequestMapping(value = "/monitor_quota_update")
	public String monitorQuotaUpdate(Model model, String id) {
		try {
			MonitorQuota vo = mqService.get(id);
			List<MonitorRule> ruleList = ruleService.list(id);
			model.addAttribute(DATA_KEY, vo);
			model.addAttribute("ruleList", ruleList);
			EnumSet<ALERT_PIPELINE> alertPipelineEnum = EnumSet.allOf (ALERT_PIPELINE. class);
			model.addAttribute("alertPipelineEnum", alertPipelineEnum);
			return "registry/monitor_quota_update";
		} catch (Exception e) {
			logger.error("获取监控指标信息失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}

	@RequestMapping(value = "/update_monitor_quota")
	@ResponseBody
	public Map<String, Object> updateMonitorQuota(Model model,
			MonitorQuotaConf vo) {
		try {
			vo.setMtime(new Date());
			if (vo.getId() == null || "".equals(vo.getId())) {
				vo.setId(UUIDGenerator.getUUID());
				mqService.insertMonitorQuota(vo);
			} else {
				mqService.updateMonitorQuota(vo);
			}
			return jsonSuccessResult("指标配置成功");
		} catch (Exception e) {
			logger.error(" 指标配置失败", e);
			return jsonFailResult(" 指标配置失败");
		}
	}
	@RequestMapping(value = "/update_status_monitor_quota")
	@ResponseBody
	public Map<String, Object> updateStatus(Model model,	String id,String status) {
		try {
				mqService.updateStatus(id,status);
			return jsonSuccessResult("修改指标状态成功");
		} catch (Exception e) {
			logger.error(" 修改指标状态失败", e);
			return jsonFailResult(" 修改指标状态失败");
		}
	}
	
}
